<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="recording and transcoding system">
    <meta name="author" content="typefo">
    <link rel="icon" href="favicon.ico">
    <title>pbxMon System</title>
    <link type="text/css" rel="stylesheet" href="/css/bootstrap.min.css">
    <link type="text/css" rel="stylesheet" href="/css/bootstrap-theme.min.css">
    <link type="text/css" rel="stylesheet" href="/css/route.css">
  </head>
  <body>
    <nav class="navbar navbar-default navbar-static-top">
      <div class="container">
        <div class="navbar-header">
          <a class="navbar-brand">
            <img alt="Brand" src="/img/logo.png" width="34" style="margin-top:-8px" />
          </a>
          <a class="navbar-brand">pbxMon</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
          <ul class="nav navbar-nav">
            <li><a href="/cdr">通话记录</a></li>
            <li><a href="/server">网关管理</a></li>
            <li><a href="/route">路由管理</a></li>
            <li><a href="/interface">接口管理</a></li>
            <li><a href="/report">数据报表</a></li>
            <li><a href="/system/status">服务器状态</a></li>
          </ul>
          <ul class="nav navbar-nav navbar-right">
            <li><a href="/system/option"><span class="glyphicon glyphicon-cog"></span> 系统设置</a></li>
            <li><a href="/logout"><span class="glyphicon glyphicon-log-out"></span> 退 出</a></li>
          </ul>
        </div>
      </div>
    </nav>
    <div class="container">
      <div class="row">
        <ol class="breadcrumb">
          <li><a href="/cdr">Home</a></li>
          <li><a href="/route">Route</a></li>
          <li class="active">Data</li>
          <li class="pull-right add"><button class="btn btn-success btn-xs" onclick="show()"><span class="glyphicon glyphicon-plus"></span> 创 建</button></li>
        </ol>
      </div>
      <div class="row">
        <table class="table table-hover table-condensed">
        	<thead><tr><th class="tab-left">#</th><th>路由名称</th><th>路由类型</th><th>路由数量</th><th>备注信息</th><th>同步操作</th><th>删 除</th><th class="tab-right">编 辑</th></tr></thead>
          <tbody>
          <?php
          if (isset($data)) {
            foreach ($data as $obj) {
              echo '<tr><td><span class="glyphicon glyphicon-list-alt"></span></td><td><a href="/dialplan?rid=',$obj['id'],'">',$obj['name'],'</a></td><td>',$obj['type'] == 1 ? '普通路由' : '高级路由','</td><td>',$obj['count'],'</td><td>',$obj['description'],'</td><td><a class="text-muted" href="javascript:void(0);" onClick="sync(',$obj['id'],",'",$obj['name'],"'",')"><span class="glyphicon glyphicon-refresh"></span> 同 步</a></td><td><a class="btn btn-default btn-xs" href="javascript:void(0);" onClick="drop(',$obj['id'],')"><span class="glyphicon glyphicon-trash"></span> 删 除</a></td><td><button class="btn btn-default btn-xs" onclick="change(',$obj['id'],')"><span class="glyphicon glyphicon-edit"></span> 编 辑</button></td></tr>';
            }
          }
          ?>
          </tbody>
        </table>
        </div>
    </div>
  </body>
  <script type="text/javascript" src="/js/jquery.min.js"></script>
  <script type="text/javascript" src="/js/bootstrap.min.js"></script>
  <script type="text/javascript" src="/js/layer.js"></script>
  <script type="text/javascript">
  function show() {
    var templete = '<form class="form-horizontal" action="/route/create" method="post" style="width:420px">'+
        '<div class="form-group">'+
        '<label class="col-sm-3 control-label">路由名称</label>'+
        '<div class="col-sm-8">'+
        '<input type="text" class="form-control" name="name" placeholder="Enter name">'+
        '</div>'+
        '</div>'+
        '<div class="form-group">'+
        '<label class="col-sm-3 control-label">路由类型</label>'+
        '<div class="col-sm-8">'+
        '<select class="form-control" name="type">'+
        '<option value="1">普通路由</option>'+
        '<option value="2">高级路由</option>'+
        '</select>'+
        '</div>'+
        '</div>'+
        '<div class="form-group">'+
        '<label class="col-sm-3 control-label">备注信息</label>'+
        '<div class="col-sm-8">'+
        '<input type="text" class="form-control" name="description" placeholder="Enter description">'+
        '</div>'+
        '</div>'+
        '<div class="form-group">'+
        '<div class="col-sm-offset-3 col-sm-8">'+
        '<button type="submit" class="btn btn-success">创 建</button><a href="javascript:void(0);" onClick="layer.closeAll()" class="btn btn-default">取 消</a>'+
        '</div>'+
        '</div>'+
        '</form>';

    layer.open({
      type: 1,
      title: '创建路由表',
      area: ['435px', '265px'],
      content: templete
    });
  }

  function change(id) {
    var url = '/route/edit?id=' + id;
    $.get(url, function(response, status){
      if (status == 'success') {
        var obj = response.data;
        var select1 = obj.type == 1 ? ' selected="selected"' : '';
        var select2 = obj.type == 2 ? ' selected="selected"' : '';
        var templete = '<form class="form-horizontal" action="/route/edit?id=' + obj.id + '" method="post" style="width:420px">'+
            '<div class="form-group">'+
            '<label class="col-sm-3 control-label">路由名称</label>'+
            '<div class="col-sm-8">'+
            '<input type="text" class="form-control" value="' + obj.name + '" disabled="disabled">'+
            '</div>'+
            '</div>'+
            '<div class="form-group">'+
            '<label class="col-sm-3 control-label">路由类型</label>'+
            '<div class="col-sm-8">'+
            '<select class="form-control" name="type">'+
            '<option value="1"' + select1 + '>普通路由</option>'+
            '<option value="2"' + select2 + '>高级路由</option>'+
            '</select>'+
            '</div>'+
            '</div>'+
            '<div class="form-group">'+
            '<label class="col-sm-3 control-label">备注信息</label>'+
            '<div class="col-sm-8">'+
            '<input type="text" class="form-control" name="description" value="' + obj.description + '" placeholder="Enter description">'+
            '</div>'+
            '</div>'+
            '<div class="form-group">'+
            '<div class="col-sm-offset-3 col-sm-8">'+
            '<button type="submit" class="btn btn-success">保 存</button><a href="javascript:void(0);" onClick="layer.closeAll()" class="btn btn-default">取 消</a>'+
            '</div>'+
            '</div>'+
            '</form>';

        layer.open({
          type: 1,
          title: '编辑路由表',
          area: ['435px', '265px'],
          content: templete
        });
      }
    });
  }

  function sync(id, name) {
    layer.confirm('亲，确定要同步 ' + name + ' 路由表？', {
        btn: ['是','否'], icon: 3
      }, function(){
        var url = '/route/sync?id=' + id;
        $.get(url, function(response, status) {
        if (status == 'success' && response.status == 200) {
          layer.msg('路由表 ' + name + ' 同步成功！', {icon: 1, time: 1000});
        } else {
          layer.alert('路由表 ' + name + ' 同步失败！', {icon: 2});
        }
      });
    });
  }

  function drop(id) {
      layer.confirm('亲，确定要删除？', {
        btn: ['是','否'], icon: 3
      }, function(){
        var url = '/route/delete?id=' + id;
        $.get(url, function(response, status){
          if (status == 'success') {
            if (response.status == 200) {
              layer.msg('删除成功!', {icon: 1, time: 1000});
              setTimeout(function() {
                window.location.reload();
              }, 1000);
            } else {
              layer.msg('抱歉，暂时无法删除!', {icon: 2});
            }
          }
        });
      });
  }
  </script>
</html>
